package com.xzx.shop_front_api.service;


import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class AlipayService {

    @Value("${alipay.returnUrl}")
    private String returnUrl;

    @Value("${alipay.notifyUrl}")
    private String notifyUrl;

    /**
     * 创建支付请求，返回支付表单HTML
     */
    public String createPay(String subject, String totalAmount, String orderNo) throws Exception {
        // 如果没有传入订单号，生成一个
        if (orderNo == null || orderNo.isEmpty()) {
            orderNo = generateOrderNo();
        }

        // 调用支付宝SDK生成支付表单
        AlipayTradePagePayResponse response = Factory.Payment.Page()
                .pay(subject, orderNo, totalAmount, returnUrl);

        // 返回支付表单HTML
        return response.getBody();
    }

    /**
     * 生成订单号
     */
    private String generateOrderNo() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 20);
    }

    /**
     * 验证回调通知的签名
     */
//    public boolean verifySign(String charset, String sign, String signType, String content) throws Exception {
//        return Factory.Payment.Common().verifyNotify(charset, sign, signType, content);
//    }
}
